<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

/**
 * 自定义基础资源类
 * Class BaseResource
 * @package App\Http\Resources
 */
class BaseResource extends JsonResource
{

    /**
     * 模式:onlg仅仅展示、except排除、default原始返回，默认：default
     * @var string
     */
    private $model = 'default';

    /**
     * 需过滤的普通字段：针对无嵌套或一对一关联模型关联
     * @var array
     */
    private $normalFields = [];

    /**
     * 需过滤的数组字段：针对一对多模型关联
     * @var array
     */
    private $arrayFields = [];

    /**
     * 拆分需过滤的普通字段、数组字段
     * @param $fields
     */
    function parseFilterFields($fields)
    {
        collect($fields)->map(function ($item, $key) {
            if (is_array($item)) {
                $this->arrayFields[$key] = $item;
                if ($this->model == "only") {
                    $this->normalFields[] = $key;
                }
            } else {
                $this->normalFields[] = $item;
            }
        });
    }

    /**
     * 仅展示字段，例：['id', 'title', 'items.name', dictionaryItems => ['id','name']]
     * 说明：items为一对一模型关联数据，dictionaryItems为一对多模型关联数据
     * @param array $fields
     * @return $this
     */
    public function only(array $fields)
    {
        $this->model = 'only';
        $this->parseFilterFields($fields);
        return $this;
    }

    /**
     * 排除字段，例：['id', 'title', 'items.name', dictionaryItems => ['id','name']]
     * 说明：items为一对一模型关联数据，dictionaryItems为一对多模型关联数据
     * @param array $fields
     * @return $this
     */
    public function except(array $fields)
    {
        $this->model = 'except';
        $this->parseFilterFields($fields);
        return $this;
    }

    /**
     * 过滤字段
     * @param $array
     * @return array
     */
    protected function filterFields($array)
    {
        if ($this->model == 'only') {
            return collect($array)->only($this->normalFields)->map(function ($item, $key) {
                $only_key = $key;
                if (is_object($item) && count($this->arrayFields) > 0) {
                    return $item->map(function ($item, $key) use ($only_key) {
                        return collect($item)->only($this->arrayFields[$only_key])->all();
                    });
                } else {
                    return $item;
                }
            })->all();
        } else if ($this->model == 'except') {
            return collect($array)->except($this->normalFields)->map(function ($item, $key) {
                $except_key = $key;
                if (is_object($item) && count($this->arrayFields) > 0) {
                    return $item->map(function ($item, $key) use ($except_key) {
                        return collect($item)->except($this->arrayFields[$except_key])->all();
                    });
                } else {
                    return $item;
                }
            })->all();
        } else {
            return $array;
        }
    }
}
